﻿using System.Drawing;
using CalcSharp.Core;
using CalcSharp.Core.Containers;

namespace plug_mtrximage.HelperClasses
{
    class OffsetFilters
    {
        public static AMatrix OffsetFilterAbs(AMatrix input, Point[,] offset, MtrxStorageType t)
        {
            AMatrix ret;
            Int96 conv = new Int96();
            Int24[] pixs = new Int24[3];
            int i, j, xOffset, yOffset;
            switch (t)
            {
                case MtrxStorageType.tint24:
                    ret = new Int24Matrix(input.Rows, input.Columns);
                    break;
                case MtrxStorageType.tfloat:
                    ret = new FloatMatrix(input.Rows, input.Columns);
                    break;
                case MtrxStorageType.tdouble:
                    ret = new DoubleMatrix(input.Rows, input.Columns);
                    break;
                default:
                    ret = new FloatMatrix(input.Rows, input.Columns);
                    break;
            }

            for (i = 0; i < ret.Rows; i++)
            {
                for (j = 0; j < ret.Columns; j++)
                {
                    xOffset = offset[j, i].X;
                    yOffset = offset[j, i].Y;
                    if (yOffset >= 0 && yOffset < input.Rows && xOffset >= 0 && xOffset < input.Columns)
                    {
                        pixs[0] = new Int24((int)input[yOffset, (xOffset * 3)]);
                        pixs[1] = new Int24((int)input[yOffset, (xOffset * 3)] + 1);
                        pixs[2] = new Int24((int)input[yOffset, (xOffset * 3)] + 2);
                        conv.R = pixs[0].R;
                        conv.B = pixs[0].G;
                        conv.G = pixs[0].B;
                        ret[i, j] = conv.ToInt32();
                    }
                }
            }
            return ret;
        }
    }
}
